<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Favorites extends MY_Controller {

    public function __construct() {
      parent::__construct();
      
      $this->load->model('users_mod');
      $this->load->model('favorites_mod');
      $this->load->model('images_mod');
    }
	/**
	 *
	 */
	public function index()
	{       
        $data['title'] = lang('favorites_title');
        $data['keywords'] = lang('favorites_keywords');
        $data['description'] = lang('favorites_description');
        try{
          $id = $this->session->userdata('user_id');
          $data['user'] = $this->users_mod->getUserData($id,false);
          $data['main_image'] = $this->images_mod->getMainImage($id);
          $data['favorites'] = $this->favorites_mod->getMyFavoritesList($id);
        } 
        catch( Exception $e )
        {
          $data['user'] = NULL;
          $data['main_image'] = NULL;
          $data['favorites'] = NULL;
        }
        
        $data['menu_active'] = "profile";        
        $data['contents']  = "favorites/home";   
		$this->load->view('template/main',$data);
	}
    
    
    /**
     *  AJAX: add_favorite
     *  Dodavanje user-a u favorites listu
     */
    public function add_favorite()
    {
        try{
          $favorite_id = $this->input->post('id'); // Favorite id        
                    
          if( true == $this->favorites_mod->add_favorite($favorite_id) ) 
          {            
            //$user_id = $this->session->userdata('user_id');
            //$data['favorites'] = $this->favorites_mod->getMyFavoritesList($user_id);
            //$html = $this->load->view('favorites/part/favorites_list',$data,true);
            $my_favorites_html = $this->load->view('modules/my_favorites',NULL,true);
            
            $side_menu['user'] = $this->users_mod->getUserData($favorite_id,false);
            $side_menu['is_friend'] = $this->friends_mod->isMyFriend($favorite_id);
            $side_menu['is_favorite'] = $this->favorites_mod->isMyFavorite($favorite_id);
            $side_menu_html = $this->load->view('profile/part/view_profile_side_menu',$side_menu,true);
            $response = array(
                        'ind' => 1,
                        'msg' => "",
                        //'num_favorites' => count($data['favorites']),
                        //'html' => $html,
                        'my_favorites_html' => $my_favorites_html,
                        'side_menu_html' => $side_menu_html
                    );
          }
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('favorite_not_added')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    /**
     *  AJAX: remove_favorite
     *  Brisanje user-a iz favorites liste
     */
    public function remove_favorite()
    {
        try{
          $favorite_id = $this->input->post('id'); // Favorite id        
                    
          if( true == $this->favorites_mod->remove_favorite($favorite_id) ) 
          {            
            $user_id = $this->session->userdata('user_id');
            $data['favorites'] = $this->favorites_mod->getMyFavoritesList($user_id);
            $html = $this->load->view('favorites/part/favorites_list',$data,true);
            $my_favorites_html = $this->load->view('modules/my_favorites',NULL,true);
            
            $side_menu['user'] = $this->users_mod->getUserData($favorite_id,false);
            $side_menu['is_friend'] = $this->friends_mod->isMyFriend($favorite_id);
            $side_menu['is_favorite'] = $this->favorites_mod->isMyFavorite($favorite_id);
            $side_menu_html = $this->load->view('profile/part/view_profile_side_menu',$side_menu,true);
            $response = array(
                        'ind' => 1,
                        'msg' => "",
                        'num_favorites' => count($data['favorites']),
                        'html' => $html,
                        'my_favorites_html' => $my_favorites_html,
                        'side_menu_html' => $side_menu_html
                    );
          }
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('favorite_not_removed')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
    public function searchFavorites()
    {
       try{         
          $name = $this->input->post('name');
                   
          $user_id = $this->session->userdata('user_id');
          $data['favorites'] = $this->favorites_mod->getMyFavoritesList($user_id,$name);
          $html = $this->load->view('favorites/part/favorites_list',$data,true);
          $my_favorites_html = $this->load->view('modules/my_favorites',NULL,true);

//          $side_menu['user'] = $this->users_mod->getUserData($favorite_id,false);
//          $side_menu['is_friend'] = $this->friends_mod->isMyFriend($favorite_id);
//          $side_menu['is_favorite'] = $this->favorites_mod->isMyFavorite($favorite_id);
//          $side_menu_html = $this->load->view('profile/part/view_profile_side_menu',$side_menu,true);
          $response = array(
                      'ind' => 1,
                      'msg' => "",
                      'num_favorites' => count($data['favorites']),
                      'html' => $html,
                      'my_favorites_html' => $my_favorites_html //,
                      //'side_menu_html' => $side_menu_html
                  );
         
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
} // end class
/* End of file favorites.php */
/* Location: ./application/controllers/favorites.php */